#ifndef __HEADER_ERRORS__
#define __HEADER_ERRORS__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crypto/log.h"

static char *error_message[] = {
    "success.",
    "out of memory!",
    "safe array size error!",
    "params error! Maybe deliver null param!",
    "out of array bounds!",
    "file operate failed!"};

enum error_code
{
    CODE_SUCCESS,
    CODE_OOM,
    CODE_SAFE_ARRAY_ERROR,
    CODE_PARAMS_ERROR,
    CODE_OUT_OF_ARRAY_BOUNDS,
    CODE_FILE_OPERATE_FAILED,
    CODE_ASSERT_ERROR,
    CODE_ERROR_LEVEL = 128,
};

#define export(code, message)                                                                                          \
    do                                                                                                                 \
    {                                                                                                                  \
        printf("\nFILE: %s LINE: %d\n  errors: %s\n  detail: %s\n", __FILE__, __LINE__, error_message[code], message); \
        if (code && code < CODE_ERROR_LEVEL)                                                                           \
        {                                                                                                              \
            exit(code);                                                                                                \
        }                                                                                                              \
    } while (0)

#define assert(exp)                  \
    do                               \
    {                                \
        if (exp)                     \
            ;                        \
        else                         \
        {                            \
            char buffer[2048];       \
            ERROR(#exp);             \
            perror("assert");        \
            exit(CODE_ASSERT_ERROR); \
        }                            \
    } while (0)
#endif